package com.swy.shopgroup;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;

import com.google.gson.Gson;
import com.swy.shopgroup.entity.ShopUser;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Response;

public class Utils {

    private static String token = "token";
    public static String shopInfo = "shopInfo";

    public static void setAllInfo(Context context, String info, String name) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(name, info);
        editor.commit();
    }

    public static String getAllInfo(Context context, String name) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        String user_token = sharedPreferences.getString(name, null);
        if (user_token == null || "".equals(user_token)) {
            return "";
        } else {
            return user_token;
        }
    }

    public static void setUserToken(Context context, String info) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(token, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(token, info);
        editor.commit();
    }

    public static ShopUser getUserToken(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(token, Context.MODE_PRIVATE);
        String user_token = sharedPreferences.getString(token, null);
        if (user_token == null || "".equals(user_token)) {
            return null;
        } else {
            ShopUser loginBean = new ShopUser();
            ShopUser.DataBean bean = new ShopUser.DataBean();
            bean.setId(-1);
            Gson gson = new Gson();
            try {
                return gson.fromJson(user_token, ShopUser.class);
            } catch (Exception e) {
                loginBean.setData(bean);
                return loginBean;
            }
        }
    }

//    public static void showDialogTag(Context context, String tag) {
//        AlertDialog.Builder alterDiaglog = new AlertDialog.Builder(context, R.style.MyDialog);
//        View vv = LayoutInflater.from(context).inflate(R.layout.dialog_face, null, false);
//        alterDiaglog.setView(vv);
//        final AlertDialog dialog = alterDiaglog.create();
//        TextView textView = vv.findViewById(R.id.tag);
//        textView.setText(tag);
//        TextView cancelBtn = vv.findViewById(R.id.confirm);
//        cancelBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        dialog.show();
//    }


    public static void checkAllPer(Activity context){
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_PHONE_STATE
                , Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (!Utils.checkMyPer(context, perms)) {
            ActivityCompat.requestPermissions(context, perms, 1);
        }
    }

    public static boolean checkMyPer(Context context, String[] perms) {
        for (int i = 0; i < perms.length; i++) {
            if (PermissionChecker.checkSelfPermission(context, perms[i]) != PermissionChecker.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

}
